package com.gljx.web.service.impl;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.constant.Constant;
import com.gljx.util.CommonUtil;
import com.gljx.util.ResourceUtil;
import com.gljx.web.dao.MenuMapper;
import com.gljx.web.Entity.Menu;
import com.gljx.web.Entity.Role;
import com.gljx.web.service.MenuService;

@Service
@Transactional
public class MenuServiceImpl implements MenuService{

	@Autowired
	private MenuMapper menuDao;
	
	@Override
	public Map<String, Object> selectBIMenu(String rows, 
											String page, 
											String menuName, 
											String menuType,
											String fisIndex,		//是否首页
											String PmenuName,
											String fisDelete) {
		Integer pageNum = Integer.valueOf(page);
		Integer pageSize = Integer.valueOf(rows);
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("Pindex", pageNum);
		String sql=  "select t.ID, t.MENU_NAME, t.MENU_URL, t.MENU_TYPE, "
				+ "t.PARENT_ID, t.CREAT_TIME, t.UPDATE_TIME, t.CREAT_ID, "
				+ "t.UPDATE_ID, t.REMARK, t.PROJECTNUMBER, t.FIS_DELETE,"
				+ "m.menu_name PmenuName ,t.FIS_INDEX fisIndex,t.IMG_URL imgUrl "
				+ "from t_e_menu t "
				+ "left join t_e_menu m on m.id=t.parent_id where t.PROJECTNUMBER = '"+Constant.Project_ID+"' ";
		if(menuName != null && !"".equals(menuName)){
			sql+= " and t.MENU_NAME like '%"+menuName+"%'" ;//菜单名
		}
		if(PmenuName != null && !"".equals(PmenuName)){
			sql+= " and m.MENU_NAME like '%"+PmenuName+"%'" ;//菜单名
		}
		//菜单类型
		if(!"no".equals(menuType)){
			sql+= " and t.MENU_TYPE ='"+menuType +"'";
		}
		//是否首页
		if(!"no".equals(fisIndex)){
			sql+= " and t.FIS_INDEX ="+fisIndex ;
		}
		//是否可用
		if(!"no".equals(fisDelete)){
			sql+= " and t.FIS_DELETE ="+fisDelete ;
		}
		data.put("Psql",sql);
		data.put("Psize", pageSize);
		
		menuDao.selectBIMenu(data);
		return data;
	}

	@Override
	public List<Menu> allSysMenu(String type) {
		
		return menuDao.allSysMenu(type,Constant.Project_ID);
	}

	@Override
	public Integer insertSelective(Menu record) {
		
		
		record.setCreatId((String)SecurityUtils.getSubject().getPrincipal());
		record.setCreatTime(CommonUtil.nowTime());
		record.setProjectnumber(Constant.Project_ID);
		return menuDao.insertSelective(record);
	}

	@Override
	public Integer updateByPrimaryKeySelective(Menu record) {
		record.setUpdateId((String)SecurityUtils.getSubject().getPrincipal());
		record.setUpdateTime(CommonUtil.nowTime());
		return menuDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public Menu selectByPrimaryKey(Integer id) {
		
		return menuDao.selectByPrimaryKey(id);
	}

	@Override
	public Integer updateMenuDelete(String ids, String delete) {
		String[] idStrings = ids.split(",");
		List<Menu> menus = new ArrayList<Menu>();
		String updateId = (String)SecurityUtils.getSubject().getPrincipal();
		//处理传入参数
		for(String string : idStrings){
			Menu menu = new Menu();
			//启用停用标记
			menu.setFisDelete(Integer.valueOf(delete));
			//修改人修改时间
			menu.setUpdateId(updateId);
			menu.setUpdateTime(CommonUtil.nowTime());
			//id
			menu.setId(Integer.valueOf(string));
			menus.add(menu);
		}
		return menuDao.updateMenuDelete(menus);
	}

	@Override
	public List<Menu> selectIndexMenu() {
		return menuDao.selectIndexMenu(
				(String)SecurityUtils.
					getSubject().getPrincipal(), 				//用户
				Constant.Project_ID,							//项目标识
				CommonUtil.subTime(Integer.valueOf(day)),		//时间
				Integer.valueOf(menuNum));						//查询条目
	}
	
	private static String day = ResourceUtil.getProper("system.timertask.day");
	
	private static String menuNum = ResourceUtil.getProper("system.indxmenu.num");

	@Override
	public List<Menu> findAllBIMenu(String PID,String id) {
		if(id != null && !"".equals(id)){
			//有id的是空根节点查询所属子节点。
			return menuDao.findAllBIMenubyID(PID,Integer.valueOf(id));
		}else{
			//这里是查询所有菜单。
			return menuDao.findAllBIMenu(PID);
		}
	}

	@Override
	public List<Menu> selectIndexTopMenu(String PID) {
		Session session = SecurityUtils.getSubject().getSession();
		Integer userId = (Integer)session.getAttribute(Constant.UserId_session);
		return menuDao.selectIndexTopMenu(PID,userId);
	};
}
